package com.zhou.springcloud.provider.controller;

import com.alibaba.fastjson.JSONPObject;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhou.springcloud.api.pojo.Dept;
import com.zhou.springcloud.provider.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhouyongjun
 * @Description: 提供RESTFUL 服务
 * @date 2021/3/20 11:38
 */
@RestController
public class DeptController {
    @Autowired
    IDeptService deptService;

    //失败调用,熔断解决方案
    @HystrixCommand(fallbackMethod = "hystrixGet")
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("id=>"+id+",不存在该用户,或者信息无法找到~");
        }
        return dept;
    }

    public Dept hystrixGet(@PathVariable("id") Long id) {
        return new Dept().setDeptno(id).setDname("id=>"+id+",不存在该用户,或者信息无法找到~").setDbSource("no this database in MYSQL");
    }

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }
    @GetMapping("dept/list")
    public List<Dept> getAll() {
        try {
            List<Dept> list = deptService.queryAll();
            System.out.println(list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
